package com.cakin;

import java.sql.*;

/**
 * @ClassName: JdbcTest2
 * @Description: jdbc测试
 * @Date: 2020/6/18
 * @Author: cakin
 */
public class JdbcTest2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // useUnicode=true&characterEncoding=utf8 解决中文乱码
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8";
        String username = "root";
        String password = "123456";

        // 1 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2 获得连接对象
        Connection connection = DriverManager.getConnection(url, username, password);
        // 3 编写SQL
        String sql = "insert into users(`id`, `name`, `password`, `email`, `birthday`) values (?,?,?,?,?)";
        // 4 预编译
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, 8);
        preparedStatement.setString(2, "cakin");
        preparedStatement.setString(3, "123456");
        preparedStatement.setString(4, "cakin@qq.com");
        preparedStatement.setDate(5, new Date(new java.util.Date().getTime()));
        // 5 执行
        preparedStatement.executeUpdate();

        // 6 关闭资源
        connection.close();
    }
}
